﻿LoadJS("/!js/uploadify-v2.1.4/swfobject.js");
LoadJS("/!js/uploadify-v2.1.4/jquery.uploadify.v2.1.4.min.js");
LoadJS("/!js/validate.js");
LoadCSS("/!js/uploadify-v2.1.4/uploadify.css");

var queuesize = 3;

//显示上传后的图片，显示数目为queuesize
function showImage(response) {
    if ($('#showQueue img').length < queuesize) {
    }
    else {
        $('#showQueue img:eq(0)').remove();
    }
    $('<img>').addClass('photo').appendTo($('#showQueue')).attr('src', response).attr('alt', ' ').attr('title', '双击删除该图片').dblclick(function () {
        $(this).remove();
        setPathList();
    }); ;
    setPathList();
    $('#hfpath').next('span').removeClass('error');
}

function setPathList() {
    var imglist = $('#showQueue img');
    var pathlist = "";
    for (var i = 0; i < imglist.length; i++) {
        if (i == 0) {
            pathlist = $(imglist[i]).attr('src');
        }
        else {
            pathlist += ',' + $(imglist[i]).attr('src');
        }
    }
    $('#hfpath').val(pathlist);
}

//初始化图片信息
function initImage() {
    var pathlist = $("#hfpath").val();
    if (pathlist != '') {
        var list = pathlist.split(',');
        for (var i = (list.length > 3 ? 3 : list.length) - 1; i >= 0; i--) {
            $('<img>').addClass('photo').appendTo($('#showQueue')).attr('src', list[i]).attr('alt', ' ').attr('title', '双击删除该图片').dblclick(function () {
                //双击删除图片 
                $(this).remove();
                setPathList();
            }); ;
        }
    }
}


$(document).ready(function () {
    var servicedate = $("[id$=hfadddate]").val().split('-');
    //图片上传
    $("#uploadify").uploadify(
        {
            'uploader': '/!js/uploadify-v2.1.4/uploadify.swf',
            'script': '/Handler/UploadHandler.ashx',
            'cancelImg': '/!js/uploadify-v2.1.4/cancel.png',
            'folder': "/UploadFile/Temp/Image/" + servicedate[0] + "/" + servicedate[1] + "/" + servicedate[2] + "/",
            'queueID': 'fileQueue',
            'auto': true,
            'multi': true,
            'fileExt': '*.jpg;*.png',
            'fileDesc': '请选择jpg , png文件...',
            'buttonImg': '/!js/uploadify-v2.1.4/btn_uploadimg.jpg',
            'wmode': 'transparent',
            'queueSizeLimit': queuesize,
            'scriptData': { 'ASPSESSID': $('#hfAspSessID').val(), 'AUTHID': $('#hfAuth').val() },
            'onComplete': function (event, queueID, fileObj, response, data) {
                if (response.indexOf('UploadFile') != -1) {
                    showImage(response);
                }
                else {
                    alert(response);
                }
            },
            'onError': function (a, b, c, d) {
                if (d.status == 404) alert('Could not find upload script. Use a path relative to: ' + '<?= getcwd() ?>');
                else if (d.type === "HTTP") alert('error ' + d.type + ": " + d.info);
                else if (d.type === "File Size") alert(c.name + ' ' + d.type + ' Limit: ' + Math.round(d.sizeLimit / 1024) + 'KB');
                else
                    alert('error ' + d.type + ": " + d.info);
            }
        }
    );
    //视频上传
    $("#videouploadify").uploadify(
        {
            'uploader': '/!js/uploadify-v2.1.4/uploadify.swf',
            'script': '/Handler/UploadHandler.ashx',
            'cancelImg': '/!js/uploadify-v2.1.4/cancel.png',
            'folder': "/UploadFile/Temp/Video/" + servicedate[0] + "/" + servicedate[1] + "/" + servicedate[2] + "/",
            'queueID': 'videoQueue',
            'auto': true,
            'multi': true,
            'fileExt': '*.avi;*.mov;*.mpg;*.mp4;*.flv;*.wmv',
            'fileDesc': '请选择avi,mov,mpg,mp4,flv文件...',
            'buttonImg': '/!js/uploadify-v2.1.4/btn_uploadvideo.jpg',
            'wmode': 'transparent',
            'queueSizeLimit': 1,
            'scriptData': { 'ASPSESSID': $('#hfAspSessID').val(), 'AUTHID': $('#hfAuth').val() },
            'onComplete': function (event, queueID, fileObj, response, data) {
                $("#hfvideopath").val(response);
                $("#hfvideopath").next('span').text("视频已上传").attr('style', 'color:green');
                $("#imgVideoTip").fadeOut(1000);
            },
            'onError': function (a, b, c, d) {
                if (d.status == 404) alert('Could not find upload script. Use a path relative to: ' + '<?= getcwd() ?>');
                else if (d.type === "HTTP") alert('error ' + d.type + ": " + d.info);
                else if (d.type === "File Size") alert(c.name + ' ' + d.type + ' Limit: ' + Math.round(d.sizeLimit / 1024) + 'KB');
                else
                    alert('error ' + d.type + ": " + d.info);
            }
        }
    );
    $('#clearQueue').click(function () {
        $('#uploadify').uploadifyClearQueue();
        return false;
    });
    //选择关键字
    $('.selectkey').click(function (e) {
        var keylist = $('#txtTags').val();
        var key = $.trim($(this).text());
        if (key != '') {
            if (keylist.indexOf(key) == -1) {
                keylist += "," + key;
                $(this).addClass('bluetxt');
            }
            else if (keylist.indexOf(key) == 0) {
                keylist = keylist.replace(key, '');
                $(this).removeClass('bluetxt');
            }
            else {
                keylist = keylist.replace(',' + key, '');
                keylist = keylist.replace(',,', ',');
                $(this).removeClass('bluetxt');
            }
        }
        if (keylist.indexOf(',') == 0) {
            keylist = keylist.substr(1, keylist.length - 1);
        }
        $('#txtTags').val(keylist);
        preventDefault(e);
    });


    //加载图片
    initImage();

    //运费
    if ($('#chkNeedShipping').is(':checked')) {
        $('#ship').show();
    }
    else {
        $('#ship').hide();
    }
    $('#chkNeedShipping').click(function () {
        if ($(this).is(':checked')) {
            $('#ship').show('inline');
        }
        $('#ship').hide();
    });
    //限时
    if ($('#ckbTimeLimited').is(':checked')) {
        $('#time').fadeIn();
    }
    else {
        $('#time').fadeOut();
    }
    $('#ckbTimeLimited').click(function () {
        if ($(this).is(':checked')) {
            $('#time').show('inline');
        }
        $('#time').hide();
    });

    //限时
    if ($('#cbNeedStock').is(':checked')) {
        $('#stock').fadeIn();
    }
    else {
        $('#stock').fadeOut();
    }
    $('#cbNeedStock').click(function () {
        if ($(this).is(':checked')) {
            $('#stock').show('inline');
        }
        $('#stock').hide();
    });


    //数据验证
    //运费
    $('#txtNativeCharge').keyup(function () {
        if (isNaN($(this).val()) | $.trim($(this).val()) == ' ') {
            $(this).val('');
        }
    });
    $('#txtOtherCharge').keyup(function () {
        if (isNaN($(this).val()) | $.trim($(this).val()) == ' ') {
            $(this).val('');
        }
    });
    $('#txtLastTime').keyup(function () {
        if (isNaN($(this).val()) | $.trim($(this).val()) == ' ') {
            $(this).val('');
        }
    });
    $('#txtStock').keyup(function () {
        if (isNaN($(this).val()) | $.trim($(this).val()) == ' ') {
            $(this).val('');
        }
    });






    //失去焦点验证
    $('#txtTitle').blur(function () {
        if ($.trim($(this).val()) == '') {
            $(this).next('span').addClass('error');
        }
        else {
            $(this).next('span').removeClass('error');
        }
    });
    $('#ddlCategory').change(function () {
        if ($('#ddlCategory option:selected').val() == 0) {
            $(this).next('span').addClass('error');
        }
        else {
            $(this).next('span').removeClass('error');
        }
    });
    $('#txtNativeCharge').blur(function () {
        if ($.trim($(this).val()) == '') {
            $(this).next('span').addClass('error');
        }
        else {
            $(this).next('span').removeClass('error');
        }
    });
    $('#txtOtherCharge').blur(function () {
        if ($.trim($(this).val()) == '') {
            $(this).next('span').addClass('error');
        }
        else {
            $(this).next('span').removeClass('error');
        }
    });
    //提交验证
    $('#ibtnSubmit').click(function (e) {
        //标题
        if ($.trim($('#txtTitle').val()) == '') {
            $('#txtTitle').focus();
            $('#txtTitle').next('span').addClass('error');
            preventDefault(e)
        }
        //ddl
        else if ($('#ddlCategory option:selected').val() == 0) {
            $('#ddlCategory').focus();
            $('#ddlCategory').next('span').addClass('error');
            preventDefault(e);
        }
        //图片
        else if ($('#hfpath').val() == '') {
            $('#hfpath').next('span').addClass('error');
            preventDefault(e);
        }
        //运费
        else if ($('#chkNeedShipping').is(':checked')) {
            if ($('#txtNativeCharge').val() == '') {
                $('#txtNativeCharge').next('span').addClass('error');
                $('#txtNativeCharge').focus();
                preventDefault(e);
            }
            else if ($('#txtOtherCharge').val() == '') {
                $('#txtOtherCharge').next('span').addClass('error');
                $('#txtOtherCharge').focus();
                preventDefault(e);
            }
        }
    });



});


